package State
{
	import ResourcePackage.ExternalResource;
	import ResourcePackage.ResourceMovieClip;
	
	import flash.display.MovieClip;
	import flash.events.*;
	import flash.net.*;
	import flash.text.*;
	import flash.utils.ByteArray;

	public class GSLogin  extends State
	{
		[Embed(source="../rsc/login.swf", mimeType="application/octet-stream")]
		public static var LoginFrame:Class;
		public var loginRscMovieClip:ResourceMovieClip = new ResourceMovieClip();
		public function GSLogin()
		{
			super();
									
		}
		
		public function checkLogin (e:MouseEvent):void {
			
			/*
			check fields before sending request to php
			*/

			if (loginRscMovieClip.GetMovieClip().username.text == "" || loginRscMovieClip.GetMovieClip().password.text == "") {
				
				/*
				if username or password fields are empty set error messages
				*/
				
				if (loginRscMovieClip.GetMovieClip().username.text == "") {
					
					loginRscMovieClip.GetMovieClip().username.text = "Enter your username";
					
				} 
				
				if (loginRscMovieClip.GetMovieClip().password.text == "") {
					
					loginRscMovieClip.GetMovieClip().password.text = "Enter your password";
					
				}
				
			} else {
				
				/*
				init function to process login
				*/
				
				processLogin();
				
			}	
		}
		
		public override function Render(g:MyGraphics):void
		{
			g.Clear(0xFF000000);
		}
		
		public override function Init():void
		{
			
			var s:Vector.<ExternalResource> = new Vector.<ExternalResource>();
			
			s.push(new ExternalResource(loginRscMovieClip, ExternalResource.TYPE_FromByteArray, new LoginFrame()));
			
			Game.GetStateStack().Push(new StateLoading("Init",s,this, false, false));
			
		}
		
		public override function Release():void
		{
			Game.GetMainWindow().removeChild(loginRscMovieClip.GetMovieClip());
		}
		
		public override function LoadComplete(success:Boolean, description:String):void
		{
			if(success)
			{
				switch(description)
				{
					case "Init":
						Game.GetMainWindow().addChild(loginRscMovieClip.GetMovieClip());
						loginRscMovieClip.GetMovieClip().submit_button.addEventListener(MouseEvent.MOUSE_DOWN, checkLogin);
						loginRscMovieClip.GetMovieClip().username.text = "player1";
						loginRscMovieClip.GetMovieClip().password.text = "12345";
						
						break;
	
				}
			}
		}
		
		
		/*
		function to process our login
		*/
		
		public function processLogin ():void {			
			loginRscMovieClip.GetMovieClip().result_text.text = "connecting..." ;
			var post:FBHttpPost = new FBHttpPost(CallBack);
			var data:String = "username=" + loginRscMovieClip.GetMovieClip().username.text + "&password=" + loginRscMovieClip.GetMovieClip().password.text;
			post.Post(Define.URLLogin,data);
			CGame.GetGame().m_bIsConnecting = true;
		}
		
		public function CallBack(str:String,b:Boolean):void{
			CGame.GetGame().m_bIsConnecting = false;
			loginRscMovieClip.GetMovieClip().result_text.autoSize = TextFieldAutoSize.LEFT;
			
			/*
			this gets the output and displays it in the result text field
			*/
			
			loginRscMovieClip.GetMovieClip().result_text.text = "" + str;
			//loginRscMovieClip.GetMovieClip().password.text = "0";
			
			if( str.indexOf("error") == -1)
			{
				var state:State = CGame.CreateState(2);
				CGame.GetGame().m_playerName = loginRscMovieClip.GetMovieClip().username.text;
				CGame.GetGame().m_sessionKey = str;
				if(state)
				{
					Game.GetStateStack().SwitchState(state,false);	
				}
			}
		}
		
	}
}